﻿using System.Collections.Generic;
using ABMath.Miscellaneous;
using MathNet.Numerics.LinearAlgebra;
using NUnit.Framework;

namespace ABMath.Tests.IridiumExtension
{
    [TestFixture]
    public class NelderMeadTest
    {
            #region Setup/Teardown

            [SetUp]
            public void SetUp()
            {
            }

            #endregion

            public double ValueFunction(Vector v)
            {
                return (v[0]*v[0] + (v[1] - 0.5)*(v[1] - 0.5));
            }

            [Test]
            public void TestIt()
            {
                var optimizer = new NelderMead();

                List<Vector> initValues = new List<Vector>();
                initValues.Add(new Vector(new[] { 1, 0.0 }));
                initValues.Add(new Vector(new[] { 0, 0.0 }));
                initValues.Add(new Vector(new[] { 1, 2.0 }));

                optimizer.Minimize(ValueFunction, initValues, 100);

                double error = optimizer.ArgMin[0]*optimizer.ArgMin[0]
                               + (optimizer.ArgMin[1] - 0.5)*(optimizer.ArgMin[1] - 0.5);
                Assert.Less(error, 1e-10);
            }
    }
}
